package main

import (
	"context"
	"core/coreinit"
	"core/db"
	"core/server/baseserver"
	"fmt"
	"iotall/iotcenter/iotcenterinit"
	"iotall/iotcore/entity"
	"time"

	mymongo "core/mongo"

	"github.com/dubbogo/gost/log/logger"
	"go.mongodb.org/mongo-driver/bson"
)

func FindBySidMongo(sid string) *entity.StdPkgDeviceMongo {
	col := mymongo.Db.Collection("StdPkgDevice")
	filter := bson.M{"_id": sid + "*001"}

	// projection := bson.D{{"_id", 1}, {"name", 1}} // 只选择field1和field2字段
	// opts := options.FindOne().SetProjection(projection)

	var tx = col.FindOne(context.TODO(), filter)
	var m = &entity.StdPkgDeviceMongo{}
	if tx.Err() == nil {
		tx.Decode(m)
		return m
	}
	return nil
}
func FindBySidMongoBatch(sid []string) []entity.StdPkgDeviceMongo {
	col := mymongo.Db.Collection("StdPkgDevice")
	filter := bson.M{"_id": bson.M{"$in": sid}}
	var r = make([]entity.StdPkgDeviceMongo, 0)
	var cur, err = col.Find(context.Background(), filter)
	if err != nil {

	}
	defer cur.Close(context.Background())
	for cur.Next(context.Background()) {
		var m entity.StdPkgDeviceMongo
		err := cur.Decode(&m)
		if err != nil {

		}
		r = append(r, m)
	}
	return r
}

func FindBySid(sid string) *entity.CommDevice {
	var m = &entity.CommDevice{}
	db.DbMaster.Model(m).Where("sid=?", sid).Find(m)
	return m
}

func Test() {
	var t1 = time.Now()
	logger.Infof("start")
	var firstid = 1
	if firstid < 10 {
		firstid *= 10000000000
	} else {
		firstid *= 1000000000
	}
	// var arr = make([]string, 0)
	for i := 0; i <= 6000; i++ {
		// var sid = strconv.Itoa(firstid+i) + ""
		// var m = FindBySidMongo(sid)
		// m.CrtDt = ""
		// var sid = strconv.Itoa(firstid+i) + "*001"
		// var m = &entity.StdPkgDeviceMongo{}
		// redis.RedisGetStruct(sid, m)
		// for index, _ := range m.PropValues {
		// 	m.PropValues[index].Fv++
		// }
		// redis.RedisSetStruct(m.Id, m, 0)

		// arr = append(arr, sid)
		// if len(arr) == 20 {
		// 	_ = FindBySidMongoBatch(arr)

		// 	arr = make([]string, 0)
		// }

		//fmt.Println(*m)
	}
	var dbnow, err = db.DbMasterNow()
	fmt.Println(dbnow)
	fmt.Print(err)

	var t2 = time.Since(t1)
	fmt.Printf("MS:%v \r\n", t2.Milliseconds())
	logger.Infof("end")

	// 要查询的值列表
	//values := []string{"10000002887*001", "10000001889*001", "10000000089*001"}

	//filter := bson.M{"_id": id}
	//var props = m.Props
	////props = append(props[:5], props[5+1:]...)
	//var sm = stream.Of(props...).
	//	Sorted(func(a, b entity.StdPkgProp2) bool {
	//		return a.Id > b.Id
	//	})
	//sm.ForEach(func(item entity.StdPkgProp2) {
	//	fmt.Println(item.Id)
	//})
	//var mup = bson.M{
	//	"CrtDt": datetime.NowStr(),
	//	"props": props,
	//}
	//update := bson.M{"$set": mup}
	//
	//var operations []mongo.WriteModel
	//operation := mongo.NewUpdateOneModel()
	//operation.SetFilter(filter)
	//operation.SetUpdate(update)
	//
	//operations = append(operations, operation)
	//collection := mymongo.Db.Collection("StdPkgDevice")
	//r, err := collection.BulkWrite(context.Background(), operations)
	//if err != nil {
	//	logger.Error(err)
	//} else {
	//	logger.Infof("InsertedCount:%v  ModifiedCount:%v UpsertedCount:%v \r\n", r.InsertedCount, r.ModifiedCount, r.UpsertedCount)
	//}
}

//	func Test2() {
//		filter := bson.M{"_id": "10000000621*001"}
//		var col = mymongo.Db.Collection("StdPkgDevice")
//		var cur, _ = col.Find(context.Background(), filter)
//		defer cur.Close(context.Background())
//		if cur.Next(context.Background()) {
//			fmt.Println("find>>>>>>>>>>>>>>")
//			var m = &entity.StdPkgDeviceMongo2{}
//			cur.Decode(m)
//			fmt.Println(m)
//			m.Uname = "abcd"
//			err := col.FindOneAndUpdate(context.Background(), filter, m)
//			fmt.Println(err)
//		}
//
// }
func main() {
	coreinit.Init()

	iotcenterinit.Init()

	baseserver.WaitCloseSignals()
}
